/*
 * Copyright (c) 2016,重庆阿莫比科技有限公司,All Rights Reserved.
 */
package com.wowocai.btchina.controller;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wowocai.btchina.entity.AmazonProductAdEntity;
import com.wowocai.btchina.service.AmazonAdsService;
import com.wowocai.btchina.service.SearchEngineCacheService;
import com.wowocai.btchina.service.SearchEngineService;

/**
 * <br/>
 * Date: 2016年12月30日 上午11:41:26 <br/>
 * 
 * @Author 刘建成
 * @Version
 */
@Controller
public class IndexController extends BaseController {

    protected final static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private SearchEngineService searchEngineService;

    @Autowired
    private AmazonAdsService amazonAdsService;

    @Autowired
    private SearchEngineCacheService searchEngineCacheService;

    private boolean isRunning = false;

    /**
     * Creates a new instance of IndexController.
     */
    public IndexController() {

    }

    @RequestMapping(value = "/")
    public String index(ModelMap model, HttpServletRequest request) {
        return hello(model, request);
    }

    @RequestMapping(value = "/index.html")
    public String hello(ModelMap model, HttpServletRequest request) {
        super.putLocale(request);
        // String country = super.getClientAdCountry(request);
        // logger.debug("广告国家编码：{}", country);
        long total = searchEngineCacheService.findTotalCount();
        NumberFormat format = DecimalFormat.getNumberInstance();
        model.put("total", format.format(total));
        List<AmazonProductAdEntity> ads = amazonAdsService.getAmazonAds(AmazonAdsService.AD_KEY_COMPUTER);
        model.put("ads", ads);
        return "index";
    }

    /**
     * 国际化切换
     * <br/>
     * 
     * @author 刘建成
     * @param model
     * @param lang
     * @return
     */
    @RequestMapping(value = "/{lang}/")
    public String i18nIndex(ModelMap model, HttpServletRequest request, @PathVariable(required = true) String lang) {
        return i18nHello(model, request, lang);
    }

    /**
     * 国际化切换
     * <br/>
     * 
     * @author 刘建成
     * @param model
     * @param lang
     * @return
     */
    @RequestMapping(value = "/{lang}/index.html")
    public String i18nHello(ModelMap model, HttpServletRequest request, @PathVariable(required = true) String lang) {
        super.changeLanguage(request, lang);
        return hello(model, request);
    }

    @RequestMapping(value = "/job/{key}/{id}.html", method = RequestMethod.GET)
    @ResponseBody
    public String job(ModelMap model, @PathVariable(required = true) Integer id,
            @PathVariable(required = true) String key) {
        if (!key.equals("80cb80092b913441346c55c7bb029840a6da0065")) {
            return "wrong key";
        }
        if (isRunning) {
            return "running";
        }
        isRunning = true;
        switch (id) {
            case 1:
                searchEngineService.pushMongoToElasticsearch();
                isRunning = false;
                return "push success";

            case 2:
                searchEngineService.syncVisitCountToEsByDate();
                isRunning = false;
                return "sync success";
            case 3:
                searchEngineService.createSiteMap();
                isRunning = false;
                return "site map success";
            case 4:
                searchEngineService.pingGoogle();
                isRunning = false;
                return "ping success";
            default:
                isRunning = false;
                return "wrong number";
        }
    }
}
